package algorithm;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: maf-poi-service
 * @description: 如果1周围都是0，那么1是孤岛
 * @author: zhouliangliang
 * @create: 2021-05-14 16:44
 **/
public class IsolatedIsland {

    private int array[][] = new int[][]{
            {0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,1,0,0,0,1,0,0,0,1},
            {0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0}};



    private void island() {
        List<String> value = new ArrayList<>();
        for(int i=0; i<array.length; i++) {
            for(int j=0; j<array[i].length; j++) {
                if(array[i][j] == 1) {
                   boolean flag = isloated(i, j, array, array.length-1, array[0].length-1);
                   if(flag) {
                       value.add(i+ "" +j);
                   }
                }
            }
        }
        value.forEach(item->{
            System.out.println(item);
        });
    }

    private void show() {
        for(int i=0; i< array.length; i++) {
            for(int j=0; j< array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }

    /**
     * 这个点的上下左右斜对角是不是0
     * @param i
     * @param j
     */
    private boolean isloated(int i, int j, int [][]array, int x, int y) {
        //上 i-1, j
        //下 i+1, j
        //左 i, j-1
        //右 i, j+1
        //左上 i-1, j-1
        //右上 i-1, j+1
        //左下 i+1, j-1
        //右下 i+1, j+1
        //判断数组没有越界
        if( (i-1<0) || (i+1>x) || (j-1< 0) || (j+1>y)) {
            return false;
        }
        //判断是孤岛
        if(array[i-1][j] == 0 &&
           array[i+1][j] == 0 &&

           array[i][j-1] == 0 &&
           array[i][j+1] == 0 &&

           array[i-1][j-1] == 0 &&
           array[i-1][j+1] == 0 &&

           array[i+1][j-1] == 0 &&
           array[i+1][j+1] == 0 ) {
            return true;
        }

        return false;
    }



    public static void main(String[] args) {
        new IsolatedIsland().island();
    }
}
